import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/login.vue'
import Home from '../components/home.vue'
import Info from '../components/info.vue'
import Analysis from '../components/analysis.vue'
import Chance from '../components/SalesManagement/chance.vue'
import Competition from '../components/SalesManagement/competition.vue'
import Foretell from '../components/SalesManagement/foretell.vue'
import Perf from '../components/SalesManagement/perf.vue'
import Plan from '../components/SalesManagement/plan.vue'





Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    component: Login,
    meta: {
      title: '登录'
    }
  },
  {
    path: '/home',
    component: Home,
    redirect: '/home/info',
    meta: {
      title: 'CRM SYSTEM'
    },
    children: [{
        path: 'info',
        component: Info,
        meta: {
          title: 'CRM SYSTEM'
        },
      }, {
        path: 'analysis',
        component: Analysis,
        meta: {
          title: 'CRM SYSTEM'
        },
      },
      {
        path: 'chance',
        component: Chance,
        meta: {
          title: 'CRM SYSTEM'
        },
      },
      {
        path: 'competition',
        component: Competition,
        meta: {
          title: 'CRM SYSTEM'
        },
      },
      {
        path: 'foretell',
        component: Foretell,
        meta: {
          title: 'CRM SYSTEM'
        },
      },
      {
        path: 'perf',
        component: Perf,
        meta: {
          title: 'CRM SYSTEM'
        },
      },
      {
        path: 'plan',
        component: Plan,
        meta: {
          title: 'CRM SYSTEM'
        },
      },
    ]
  },
]

const router = new VueRouter({
  routes
})

//全局前置路由守卫————初始化的时候被调用、每次路由切换之前被调用
router.beforeEach((to, from, next) => {
  const tokenStr = sessionStorage.getItem('token')
  if (to.path == '/login') {
    if (tokenStr) {
      console.log(1);
      return next('/home')
    }
    return next()
  }
  if (to.path == '/home/welcome') sessionStorage.setItem('activePath', 'info')
  if (!tokenStr) return next('/login')
  next()
})

router.afterEach((to, from) => {
  document.title = to.meta.title
})
export default router
